package com.bayair.doctor.mvp;

import android.content.Intent;
import android.os.Bundle;


import com.bayair.doctor.base.component.BaseActivity;
import com.bayair.doctor.mvp.proxy.ProxyActivity;
import com.bayair.doctor.ui.login.LoginActivity;
import com.bayair.doctor.utils.ActivityStackManager;

import androidx.annotation.Nullable;

import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;

/**
 * Author: tuyrt7
 * Date: 2019/9/28 20:33
 * Description:
 * 在子类直接使用@InjectPresenter 注解
 */
public abstract class BaseMVPActivity extends BaseActivity implements IView {

    private ProxyActivity mProxyActivity;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        mProxyActivity = createProxyActivity();
        mProxyActivity.bindPresenter();

        super.onCreate(savedInstanceState);
    }

    private ProxyActivity createProxyActivity() {
        if (mProxyActivity == null) {
            return new ProxyActivity(this);
        }
        return mProxyActivity;
    }

    @Override
    protected void onDestroy() {
        if (mProxyActivity != null) {
            mProxyActivity.unbindPresenter();
        }
        super.onDestroy();
    }


    @Override
    public void onLoading() {
        showLoading();
    }

    @Override
    public void onComplete() {
        showComplete();
    }

    @Override
    public void onEmpty() {
        //showEmpty();//打开必须在xml中有HintLayout
    }

    @Override
    public void onError() {
        //showError();//打开必须在xml中有HintLayout
    }

    /**
     * token过期，去登录页面
     */
    @Override
    public void tokenExpire() {
        toast("登录过期，请重新登录");
        Intent intent = new Intent(getActivity(), LoginActivity.class);
        intent.addFlags(FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        ActivityStackManager.getInstance().finishAllActivities(LoginActivity.class);
    }
}
